/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package game;

/**
 *
 * @author Toan
 */
import java.net.*;
import java.io.*;
import java.util.*;

/**
 *
 * @author Toan
 */
public class TriviaThread extends Thread {
    Socket socket = null;
    public TriviaThread(Socket soc){
        this.socket = soc;
    }
    class Trivia {
    String question;
    String answer;
    }

    Trivia trivia[] = new Trivia[5];

    TriviaThread() {
     for (int i = 0; i < 5; i++) {
     trivia[i] = new Trivia();
    }
    trivia[0].question =
    "How many books are there in the Old Testament?";
    trivia[1].question = "What is the first book in the Old Testament?";
    trivia[2].question = "What is the first book in the New Testament?";
    trivia[3].question = "Who was the first man created by God?";
    trivia[4].question = "How many children did Noah have?";
    trivia[0].answer = "39";
    trivia[1].answer = "genesis";
    trivia[2].answer = "matthew";
    trivia[3].answer = "adam";
    trivia[4].answer = "3";
    }

    String processMsg(String msg) {
       if (msg.equals("bye")) {
        return "bye";
       } else if(msg.equals("request")) {
            int num = (int) (Math.random()*100);
             return (num%5+1) + ": " + trivia[num%5].question;
     } else if (msg.equals("answer")) {
/* ask for answer*/
     return "Give your answer in this format:<question number>#<your answer>";
    } else {
        int questionNum = 0;
        String ans = "";
/* check if answer was correct */
        StringTokenizer st = new StringTokenizer(msg, "#\n");
/* get question number */
        if (st.hasMoreTokens()) {
        try {
           questionNum = Integer.parseInt(st.nextToken()) - 1;
        } catch (Exception e) {
        return "Incorrect format for answer.";
        }
        } else {
        return "Incorrect format for answer.";
        }
//SERVER continued on next page
/* get answer */
        if (st.hasMoreTokens()) {
            ans = st.nextToken().trim();
        if (trivia[questionNum].answer.equalsIgnoreCase(ans)) {
            return "Good job!";
        } else {
            return "Sorry that's incorrect.";
                }
           }
                return "Incorrect format for answer.";
       }
}
    @Override
    public void run(){
    TriviaThread ts = new TriviaThread();
    ServerSocket server = null;
    Socket client;
    
/*
    try {
    server = new ServerSocket(1234);
    } catch (IOException ie) {
    System.out.println("Cannot open socket.");
        System.exit(1);
    }
*/
    while(true) {
        try {
     
   // client = server.accept();
    OutputStream clientOut = socket.getOutputStream();
    PrintWriter pw = new PrintWriter(clientOut, true);
    InputStream clientIn = socket.getInputStream();
    BufferedReader br = new BufferedReader(new
    InputStreamReader(clientIn));
    String msg = br.readLine();
    if (msg!=null) {
    String ansMsg = ts.processMsg(msg);
    System.out.println("Sending msg: " + ansMsg);
    pw.println(ansMsg);
}
} catch (IOException ie) {
}
}
}
    }

